<header class="flex items-center mb-6">
  <div>
    <h1 class="crayons-title">Scheduled Automations for <%= @bot.name %></h1>
    <p class="color-secondary">Manage scheduled automations for this community bot</p>
  </div>
  <div class="ml-auto flex gap-2">
    <%= link_to "New Automation", new_admin_subforem_community_bot_scheduled_automation_path(@subforem, @bot), class: "crayons-btn crayons-btn--s" %>
    <%= link_to "Back to Bot", admin_subforem_community_bot_path(@subforem, @bot), class: "crayons-btn crayons-btn--outlined crayons-btn--s" %>
  </div>
</header>

<div class="crayons-card p-6">
  <% if @automations.empty? %>
    <div class="text-center py-8">
      <p class="color-secondary">No scheduled automations found for this bot.</p>
      <%= link_to "Create your first automation", new_admin_subforem_community_bot_scheduled_automation_path(@subforem, @bot), class: "crayons-btn mt-4" %>
    </div>
  <% else %>
    <div class="grid gap-4">
      <% @automations.each do |automation| %>
        <div class="border border-base-20 rounded-lg p-4 <%= 'opacity-50' unless automation.enabled? %>" data-testid="scheduled-automation">
          <div class="flex items-start justify-between mb-3">
            <div class="flex-1">
              <div class="flex items-center gap-2 mb-2">
                <h3 class="font-semibold text-lg">
                  <%= automation.service_name.titleize %> → <%= automation.action.humanize %>
                </h3>
                <% unless automation.enabled? %>
                  <span class="crayons-tag crayons-tag--outlined">Disabled</span>
                <% end %>
                <% if automation.state == "running" %>
                  <span class="crayons-tag crayons-tag--filled" style="background-color: #3b49df;">Running</span>
                <% elsif automation.state == "failed" %>
                  <span class="crayons-tag crayons-tag--filled" style="background-color: #dc2626;">Failed</span>
                <% end %>
              </div>
              
              <div class="text-sm color-secondary space-y-1">
                <div><strong>Frequency:</strong> <%= format_frequency(automation) %></div>
                <% if automation.next_run_at %>
                  <div><strong>Next Run:</strong> <%= automation.next_run_at.strftime("%b %d, %Y at %I:%M %p %Z") %></div>
                <% end %>
                <% if automation.last_run_at %>
                  <div><strong>Last Run:</strong> <%= time_ago_in_words(automation.last_run_at) %> ago</div>
                <% end %>
                <% if automation.additional_instructions.present? %>
                  <div class="mt-2 p-2 bg-base-10 rounded">
                    <strong>Additional Instructions:</strong>
                    <p class="mt-1"><%= truncate(automation.additional_instructions, length: 150) %></p>
                  </div>
                <% end %>
              </div>
            </div>
            
            <div class="flex gap-2">
              <%= link_to automation.enabled? ? "Disable" : "Enable", 
                  toggle_enabled_admin_subforem_community_bot_scheduled_automation_path(@subforem, @bot, automation),
                  method: :post,
                  class: "crayons-btn crayons-btn--s crayons-btn--outlined" %>
              <%= link_to "Edit", edit_admin_subforem_community_bot_scheduled_automation_path(@subforem, @bot, automation), 
                  class: "crayons-btn crayons-btn--s" %>
              <%= link_to "Delete", admin_subforem_community_bot_scheduled_automation_path(@subforem, @bot, automation), 
                  method: :delete, 
                  data: { confirm: "Are you sure you want to delete this automation? This action cannot be undone." },
                  class: "crayons-btn crayons-btn--danger crayons-btn--s" %>
            </div>
          </div>

          <% if automation.action_config.present? && automation.action_config.any? %>
            <div class="mt-3 p-3 bg-base-10 rounded">
              <h4 class="text-sm font-semibold mb-2">Action Configuration</h4>
              <div class="text-xs font-mono">
                <% automation.action_config.each do |key, value| %>
                  <div><strong><%= key %>:</strong> <%= value %></div>
                <% end %>
              </div>
            </div>
          <% end %>
        </div>
      <% end %>
    </div>
  <% end %>
</div>

<% content_for :head do %>
  <style>
    .opacity-50 {
      opacity: 0.5;
    }
  </style>
<% end %>

